<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的数据库迁移-migrations" tabindex="-1"><a class="header-anchor" href="#laravel-的数据库迁移-migrations"><span>Laravel 的数据库迁移 Migrations</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#generating-migrations">生成迁移</a></li>
<li><a href="#migration-structure">迁移结构</a></li>
<li><a href="#running-migrations">运行迁移</a>
<ul>
<li><a href="#rolling-back-migrations">回滚迁移</a></li>
</ul>
</li>
<li><a href="#tables">数据表</a>
<ul>
<li><a href="#creating-tables">创建数据表</a></li>
<li><a href="#renaming-and-dropping-tables">重命名/删除数据表</a></li>
</ul>
</li>
<li><a href="#columns">字段</a>
<ul>
<li><a href="#creating-columns">创建字段</a></li>
<li><a href="#column-modifiers">字段修饰</a></li>
<li><a href="#modifying-columns">修改字段</a></li>
<li><a href="#dropping-columns">删除字段</a></li>
</ul>
</li>
<li><a href="#indexes">索引</a>
<ul>
<li><a href="#creating-indexes">创建索引</a></li>
<li><a href="#dropping-indexes">删除索引</a></li>
<li><a href="#foreign-key-constraints">外键约束</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>数据库迁移就像是数据库的版本控制，可以让你的团队轻松修改并共享应用程序的数据库结构。迁移通常与 Laravel 的数据库结构生成器配合使用，让你轻松地构建数据库结构。如果你曾经试过让同事手动在数据库结构中添加字段，那么数据库迁移可以让你不再需要做这样的事情。</p>
<p>Laravel <code v-pre>Schema</code> <a href="https://learnku.com/docs/laravel/5.5/facades" target="_blank" rel="noopener noreferrer">facade</a> 对所有 Laravel 支持的数据库系统提供了创建和操作数据表的相应支持。</p>
<h2 id="生成迁移" tabindex="-1"><a class="header-anchor" href="#生成迁移"><span>生成迁移</span></a></h2>
<p>使用 <a href="https://learnku.com/docs/laravel/5.5/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> <code v-pre>make:migration</code> 来创建迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>migration create_users_table</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>新的迁移文件会被放置在 <code v-pre>database/migrations</code> 目录中。每个迁移文件的名称都包含了一个时间戳，以便让 Laravel 确认迁移的顺序。</p>
<p><code v-pre>--table</code> 和 <code v-pre>--create</code> 选项可用来指定数据表的名称，或是该迁移被执行时是否将创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>migration create_users_table <span class="token operator">--</span>create<span class="token operator">=</span>users</span>
<span class="line"></span>
<span class="line">php artisan make<span class="token punctuation">:</span>migration add_votes_to_users_table <span class="token operator">--</span>table<span class="token operator">=</span>users</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想为生成的迁移指定一个自定义输出路径，则可以在运行 <code v-pre>make:migration</code> 命令时添加 <code v-pre>--path</code> 选项。给定的路径必须是相对于应用程序的基本路径。</p>
<h2 id="迁移结构" tabindex="-1"><a class="header-anchor" href="#迁移结构"><span>迁移结构</span></a></h2>
<p>迁移类通常会包含两个方法：<code v-pre>up</code> 和 <code v-pre>down</code>。<code v-pre>up</code> 方法可为数据库添加新的数据表、字段或索引，而 <code v-pre>down</code> 方法则是 <code v-pre>up</code> 方法的逆操作。</p>
<p>你可以在这两个方法中使用 Laravel 数据库结构生成器来创建以及修改数据表。若要了解 <code v-pre>Schema</code> 生成器中的所有可用方法，<a href="#creating-tables">可查阅它的文档</a>。以下的迁移实例会创建一张 <code v-pre>flights</code> 数据表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Migrations<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CreateFlightsTable</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 运行数据库迁移</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">increments</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'airline'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 回滚数据库迁移</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">drop</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="运行迁移" tabindex="-1"><a class="header-anchor" href="#运行迁移"><span>运行迁移</span></a></h2>
<p>使用 Artisan 命令 <code v-pre>migrate</code> 来运行所有未完成的迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} 如果你使用的是 <a href="https://learnku.com/docs/laravel/5.5/homestead" target="_blank" rel="noopener noreferrer">Homestead 虚拟机</a>，则应该在虚拟机中执行命令。</p>
</blockquote>
<h4 id="在生产环境强制执行迁移" tabindex="-1"><a class="header-anchor" href="#在生产环境强制执行迁移"><span>在生产环境强制执行迁移</span></a></h4>
<p>一些迁移操作是具有破坏性的，这意味着可能会导致数据丢失。为了防止有人在生产环境中运行这些命令，系统会在这些命令被运行之前与你进行确认。如果要强制忽略系统的提示运行命令，则可以使用 <code v-pre>--force</code> 标记：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate <span class="token operator">--</span>force</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="回滚迁移" tabindex="-1"><a class="header-anchor" href="#回滚迁移"><span>回滚迁移</span></a></h3>
<p>若要回滚最后一次迁移，可以使用 <code v-pre>rollback</code> 命令。此命令将回滚最后一次「迁移」，其中可能包含多个迁移文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate<span class="token punctuation">:</span>rollback</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在 <code v-pre>rollback</code> 命令后加上 <code v-pre>step</code> 参数，你可以限制回滚迁移的个数。例如，以下命令将回滚最近五次迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate<span class="token punctuation">:</span>rollback <span class="token operator">--</span>step<span class="token operator">=</span><span class="token number">5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>migrate:reset</code> 命令可以回滚应用程序中的所有迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate<span class="token punctuation">:</span>reset</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="使用单个命令来执行回滚或迁移" tabindex="-1"><a class="header-anchor" href="#使用单个命令来执行回滚或迁移"><span>使用单个命令来执行回滚或迁移</span></a></h4>
<p><code v-pre>migrate:refresh</code> 命令不仅会回滚数据库的所有迁移还会接着运行 <code v-pre>migrate</code> 命令。这个命令可以高效地重建整个数据库：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate<span class="token punctuation">:</span>refresh</span>
<span class="line"></span>
<span class="line"><span class="token comment">// 刷新数据库结构并执行数据填充</span></span>
<span class="line">php artisan migrate<span class="token punctuation">:</span>refresh <span class="token operator">--</span>seed</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 <code v-pre>refresh</code> 命令并提供 <code v-pre>step</code> 参数来回滚并再执行最后指定的迁移数。例如，以下命令将回滚并重新执行最后五次迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate<span class="token punctuation">:</span>refresh <span class="token operator">--</span>step<span class="token operator">=</span><span class="token number">5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="删除所有表-迁移" tabindex="-1"><a class="header-anchor" href="#删除所有表-迁移"><span>删除所有表 &amp; 迁移</span></a></h4>
<p><code v-pre>migrate:fresh</code> 命令会从数据库中删除所有表，然后执行 <code v-pre>migrate</code> 命令:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate<span class="token punctuation">:</span>fresh</span>
<span class="line"></span>
<span class="line">php artisan migrate<span class="token punctuation">:</span>fresh <span class="token operator">--</span>seed</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数据表" tabindex="-1"><a class="header-anchor" href="#数据表"><span>数据表</span></a></h2>
<h3 id="创建数据表" tabindex="-1"><a class="header-anchor" href="#创建数据表"><span>创建数据表</span></a></h3>
<p>可以使用 <code v-pre>Schema</code> facade 的 <code v-pre>create</code> 方法来创建新的数据库表。<code v-pre>create</code> 方法接受两个参数：第一个参数为数据表的名称，第二个参数是 <code v-pre>闭包</code>，此闭包会接收一个用于定义新数据表的 <code v-pre>Blueprint</code> 对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">increments</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，在创建数据表的时候，可以使用任何数据库结构生成器的 <a href="#creating-columns">字段方法</a> 来定义数据表的字段。</p>
<h4 id="检查数据表或字段是否存在" tabindex="-1"><a class="header-anchor" href="#检查数据表或字段是否存在"><span>检查数据表或字段是否存在</span></a></h4>
<p>可以使用 <code v-pre>hasTable</code> 和 <code v-pre>hasColumn</code> 方法来检查数据表或字段是否存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="数据库连接-表选项" tabindex="-1"><a class="header-anchor" href="#数据库连接-表选项"><span>数据库连接 &amp; 表选项</span></a></h4>
<p>如果要对非默认连接的数据库连接执行结构操作，可以使用 <code v-pre>connection</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">increments</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以在数据库结构生成器上使用以下命令来定义表的选项：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;engine = 'InnoDB';</code></td>
<td>指定表存储引擎 (MySQL)</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;charset = 'utf8';</code></td>
<td>指定数据表的默认字符集 (MySQL)</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;collation = 'utf8_unicode_ci';</code></td>
<td>指定数据表默认的排序规则 (MySQL)</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;temporary();</code></td>
<td>创建临时表(不支持SQL Server).</td>
</tr>
</tbody>
</table>
<h3 id="重命名-删除数据表" tabindex="-1"><a class="header-anchor" href="#重命名-删除数据表"><span>重命名/删除数据表</span></a></h3>
<p>若要重命名数据表，可以使用 <code v-pre>rename</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">rename</span><span class="token punctuation">(</span><span class="token variable">$from</span><span class="token punctuation">,</span> <span class="token variable">$to</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>要删除数据表，可使用 <code v-pre>drop</code> 或 <code v-pre>dropIfExists</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">drop</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">dropIfExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="重命名带外键的数据表" tabindex="-1"><a class="header-anchor" href="#重命名带外键的数据表"><span>重命名带外键的数据表</span></a></h4>
<p>在重命名表之前，你应该验证表上的任何外键约束在迁移文件中都有明确的名称，而不是让 Laravel 按照约定来设置一个名称。否则，外键的约束名称将引用旧表名。</p>
<h2 id="字段" tabindex="-1"><a class="header-anchor" href="#字段"><span>字段</span></a></h2>
<h3 id="创建字段" tabindex="-1"><a class="header-anchor" href="#创建字段"><span>创建字段</span></a></h3>
<p>使用 <code v-pre>Schema</code> facade 的 <code v-pre>table</code> 方法可以更新现有的数据表。如同 <code v-pre>create</code> 方法一样，<code v-pre>table</code> 方法会接受两个参数：一个是数据表的名称，另一个则是接收可以用来向表中添加字段的 <code v-pre>Blueprint</code> 实例的<code v-pre>闭包</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="可用的字段类型" tabindex="-1"><a class="header-anchor" href="#可用的字段类型"><span>可用的字段类型</span></a></h4>
<p>数据库结构生成器包含构建表时可以指定的各种字段类型：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;bigIncrements('id');</code></td>
<td>递增 ID（主键），相当于「UNSIGNED BIG INTEGER」</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;bigInteger('votes');</code></td>
<td>相当于 BIGINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;binary('data');</code></td>
<td>相当于 BLOB</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;boolean('confirmed');</code></td>
<td>相当于 BOOLEAN</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;char('name', 4);</code></td>
<td>相当于带有长度的 CHAR</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;date('created_at');</code></td>
<td>相当于 DATE</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dateTime('created_at');</code></td>
<td>相当于 DATETIME</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dateTimeTz('created_at');</code></td>
<td>相当于带时区 DATETIME</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;decimal('amount', 8, 2);</code></td>
<td>相当于带有精度与基数 DECIMAL</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;double('column', 8, 2);</code></td>
<td>相当于带有精度与基数 DOUBLE</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;enum('level', ['easy', 'hard']);</code></td>
<td>相当于 ENUM</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;float('amount', 8, 2);</code></td>
<td>相当于带有精度与基数 FLOAT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;geometry('positions');</code></td>
<td>相当于 GEOMETRY</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;geometryCollection('positions');</code></td>
<td>相当于 GEOMETRYCOLLECTION</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;increments('id');</code></td>
<td>递增的 ID (主键)，相当于「UNSIGNED INTEGER」</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;integer('votes');</code></td>
<td>相当于 INTEGER</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;ipAddress('visitor');</code></td>
<td>相当于 IP 地址</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;json('options');</code></td>
<td>相当于 JSON</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;jsonb('options');</code></td>
<td>相当于 JSONB</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;lineString('positions');</code></td>
<td>相当于 LINESTRING</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;longText('description');</code></td>
<td>相当于 LONGTEXT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;macAddress('device');</code></td>
<td>相当于 MAC 地址</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;mediumIncrements('id');</code></td>
<td>递增 ID (主键) ，相当于「UNSIGNED MEDIUM INTEGER」</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;mediumInteger('votes');</code></td>
<td>相当于 MEDIUMINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;mediumText('description');</code></td>
<td>相当于 MEDIUMTEXT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;morphs('taggable');</code></td>
<td>相当于加入递增的 <code v-pre>taggable_id</code> 与字符串 <code v-pre>taggable_type</code></td>
</tr>
<tr>
<td><code v-pre>$table-&gt;multiLineString('positions');</code></td>
<td>相当于 MULTILINESTRING</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;multiPoint('positions');</code></td>
<td>相当于 MULTIPOINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;multiPolygon('positions');</code></td>
<td>相当于 MULTIPOLYGON</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;nullableMorphs('taggable');</code></td>
<td>相当于可空版本的 <code v-pre>morphs()</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;nullableTimestamps();</code></td>
<td>相当于可空版本的 <code v-pre>timestamps()</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;point('position');</code></td>
<td>相当于 POINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;polygon('positions');</code></td>
<td>相当于 POLYGON</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;rememberToken();</code></td>
<td>相当于可空版本的 VARCHAR(100) 的 <code v-pre>remember_token</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;smallIncrements('id');</code></td>
<td>递增 ID (主键) ，相当于「UNSIGNED SMALL INTEGER」</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;smallInteger('votes');</code></td>
<td>相当于 SMALLINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;softDeletes();</code></td>
<td>相当于为软删除添加一个可空的 <code v-pre>deleted_at</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;softDeletesTz();</code></td>
<td>相当于为软删除添加一个可空的 带时区的 <code v-pre>deleted_at</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;string('name', 100);</code></td>
<td>相当于带长度的 VARCHAR</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;text('description');</code></td>
<td>相当于 TEXT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;time('sunrise');</code></td>
<td>相当于 TIME</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;timeTz('sunrise');</code></td>
<td>相当于带时区的 TIME</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;timestamp('added_on');</code></td>
<td>相当于 TIMESTAMP</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;timestampTz('added_on');</code></td>
<td>相当于带时区的 TIMESTAMP</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;tinyIncrements('id');</code></td>
<td>相当于自动递增 UNSIGNED TINYINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;tinyInteger('votes');</code></td>
<td>相当于 TINYINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unsignedBigInteger('votes');</code></td>
<td>相当于 Unsigned BIGINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unsignedDecimal('amount', 8, 2);</code></td>
<td>相当于带有精度和基数的 UNSIGNED DECIMAL</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unsignedInteger('votes');</code></td>
<td>相当于 Unsigned INT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unsignedMediumInteger('votes');</code></td>
<td>相当于 Unsigned MEDIUMINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unsignedSmallInteger('votes');</code></td>
<td>相当于 Unsigned SMALLINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unsignedTinyInteger('votes');</code></td>
<td>相当于 Unsigned TINYINT</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;uuid('id');</code></td>
<td>相当于 UUID</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;year('birth_year');</code></td>
<td>相当于 YEAR</td>
</tr>
</tbody>
</table>
<h3 id="字段修饰" tabindex="-1"><a class="header-anchor" href="#字段修饰"><span>字段修饰</span></a></h3>
<p>除了上述列出的字段类型之外，还有几个可以在添加字段时使用的「修饰符」。例如，如果要把字段设置为「可空」，就使用 <code v-pre>nullable</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">nullable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>以下是所有可用的字段修饰符的列表。此列表不包括 <a href="#creating-indexes">索引修饰符</a>：</p>
<table>
<thead>
<tr>
<th>修饰符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>-&gt;after('column')</code></td>
<td>将此字段放置在其它字段「之后」（仅限 MySQL）</td>
</tr>
<tr>
<td><code v-pre>-&gt;autoIncrement()</code></td>
<td>将 INTEGER 类型的字段设置为自动递增的主键</td>
</tr>
<tr>
<td><code v-pre>-&gt;charset('utf8')</code></td>
<td>指定一个字符集（仅限 MySQL）</td>
</tr>
<tr>
<td><code v-pre>-&gt;collation('utf8_unicode_ci')</code></td>
<td>指定列的排序规则（仅限 MySQL/SQL Server）</td>
</tr>
<tr>
<td><code v-pre>-&gt;comment('my comment')</code></td>
<td>为字段增加注释（仅限 MySQL）</td>
</tr>
<tr>
<td><code v-pre>-&gt;default($value)</code></td>
<td>为字段指定「默认」值</td>
</tr>
<tr>
<td><code v-pre>-&gt;first()</code></td>
<td>将此字段放置在数据表的「首位」（仅限 MySQL）</td>
</tr>
<tr>
<td><code v-pre>-&gt;nullable($value = true)</code></td>
<td>（默认情况下）此字段允许写入 NULL 值</td>
</tr>
<tr>
<td><code v-pre>-&gt;storedAs($expression)</code></td>
<td>创建一个存储生成的字段 （仅限 MySQL）</td>
</tr>
<tr>
<td><code v-pre>-&gt;unsigned()</code></td>
<td>设置 <code v-pre>integer</code> 类型的字段为 <code v-pre>UNSIGNED</code>（仅限 MySQL）</td>
</tr>
<tr>
<td><code v-pre>-&gt;useCurrent()</code></td>
<td>将 TIMESTAMP 类型的字段设置为使用 CURRENT_TIMESTAMP 作为默认值</td>
</tr>
<tr>
<td><code v-pre>-&gt;virtualAs($expression)</code></td>
<td>创建一个虚拟生成的字段 （仅限 MySQL）</td>
</tr>
</tbody>
</table>
<h3 id="修改字段" tabindex="-1"><a class="header-anchor" href="#修改字段"><span>修改字段</span></a></h3>
<h4 id="先决条件" tabindex="-1"><a class="header-anchor" href="#先决条件"><span>先决条件</span></a></h4>
<p>在修改字段之前，请确保将 <code v-pre>doctrine/dbal</code> 依赖添加到 <code v-pre>composer.json</code> 文件中。Doctrine DBAL 库用于确定字段的当前状态，并创建对该字段进行指定调整所需的 SQL 查询：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> doctrine<span class="token operator">/</span>dbal</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="更新字段属性" tabindex="-1"><a class="header-anchor" href="#更新字段属性"><span>更新字段属性</span></a></h4>
<p><code v-pre>change</code> 方法可以将现有的字段类型修改为新的类型或修改属性。比如，你可能想增加字符串字段的长度，可以使用 <code v-pre>change</code> 方法把 <code v-pre>name</code> 字段的长度从 25 增加到 50 ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将字段修改为可空：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">nullable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{note} 只有下面的字段类型能被「修改」: bigInteger、binary、boolean、date、dateTime、dateTimeTz、 decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger 和 unsignedSmallInteger。</p>
</blockquote>
<h4 id="重命名字段" tabindex="-1"><a class="header-anchor" href="#重命名字段"><span>重命名字段</span></a></h4>
<p>可以使用结构生成器上的 <code v-pre>renameColumn</code> 方法来重命名字段。在重命名字段前，请确保你的 <code v-pre>composer.json</code> 文件内已经加入 <code v-pre>doctrine/dbal</code> 依赖：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">renameColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{note} 当前不支持 <code v-pre>enum</code> 类型的字段重命名。</p>
</blockquote>
<h3 id="删除字段" tabindex="-1"><a class="header-anchor" href="#删除字段"><span>删除字段</span></a></h3>
<p>可以使用结构生成器上的 <code v-pre>dropColumn</code> 方法来删除字段。在从 SQLite 数据库删除字段前，你需要在 <code v-pre>composer.json</code> 文件中加入 <code v-pre>doctrine/dbal</code> 依赖并在终端执行 <code v-pre>composer update</code> 来安装该依赖：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以传递一个字段数组给 <code v-pre>dropCloumn</code> 方法来删除多个字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'location'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{note} 不支持在使用 SQLite 数据库时在单个迁移中删除或修改多个字段。</p>
</blockquote>
<h4 id="可用的命令别名" tabindex="-1"><a class="header-anchor" href="#可用的命令别名"><span>可用的命令别名</span></a></h4>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;dropRememberToken();</code></td>
<td>删除 <code v-pre>remember_token</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSoftDeletes();</code></td>
<td>删除 <code v-pre>deleted_at</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSoftDeletesTz();</code></td>
<td><code v-pre>dropSoftDeletes()</code> 方法的别名</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropTimestamps();</code></td>
<td>删除 <code v-pre>created_at</code> 和 <code v-pre>updated_at</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropTimestampsTz();</code></td>
<td><code v-pre>dropTimestamps()</code> 方法的别名</td>
</tr>
</tbody>
</table>
<h2 id="索引" tabindex="-1"><a class="header-anchor" href="#索引"><span>索引</span></a></h2>
<h3 id="创建索引" tabindex="-1"><a class="header-anchor" href="#创建索引"><span>创建索引</span></a></h3>
<p>结构生成器支持多种类型的索引。首先，先指定字段值唯一，即简单地在字段定义之后链式调用 <code v-pre>unique</code> 方法来创建索引，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，你也可以在定义完字段之后创建索引。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你甚至可以将数组传递给索引方法来创建一个复合（或合成）索引：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 会自动生成一个合理的索引名称，但你也可以传递第二个参数来自定义索引名称:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique_email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="可用的索引类型" tabindex="-1"><a class="header-anchor" href="#可用的索引类型"><span>可用的索引类型</span></a></h4>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;primary('id');</code></td>
<td>添加主键</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;primary(['id', 'parent_id']);</code></td>
<td>添加复合键</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unique('email');</code></td>
<td>添加唯一索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;index('state');</code></td>
<td>添加普通索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;spatialIndex('location');</code></td>
<td>添加空间索引（ SQLite 除外）</td>
</tr>
</tbody>
</table>
<h4 id="索引长度-mysql-mariadb" tabindex="-1"><a class="header-anchor" href="#索引长度-mysql-mariadb"><span>索引长度 &amp; MySQL / MariaDB</span></a></h4>
<p>Laravel 默认使用 <code v-pre>utf8mb4</code> 字符，它支持在数据库中存储「emojis」。如果你是在版本低于5.7.7 的 MySQL release 或者版本低于10.2.2 的 MariaDB release 上创建索引，那就需要你手动配置迁移生成的默认字符串长度。即在 <code v-pre>AppServiceProvider</code> 中调用 <code v-pre>Schema::defaultStringLength</code> 方法来配置它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">defaultStringLength</span><span class="token punctuation">(</span><span class="token number">191</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以开启数据库的 <code v-pre>innodb_large_prefix</code> 选项，至于如何正确开启，请自行查阅数据库文档。</p>
<h3 id="删除索引" tabindex="-1"><a class="header-anchor" href="#删除索引"><span>删除索引</span></a></h3>
<p>若要移除索引，则必须指定索引的名称。Laravel 默认会自动给索引分配合理的名称。其将数据表名称、索引的字段名称及索引类型简单地连接在了一起。举例如下：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;dropPrimary('users_id_primary');</code></td>
<td>从「users」表中删除主键。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropUnique('users_email_unique');</code></td>
<td>从「users」表中删除唯一索引。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropIndex('geo_state_index');</code></td>
<td>从「geo」表中删除基本索引。</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSpatialIndex('geo_location_spatialindex');</code></td>
<td>从「geo」表中删除空间索引（SQLite除外）。</td>
</tr>
</tbody>
</table>
<p>如果将字段数组传递给 <code v-pre>dropIndex</code> 方法，会删除根据表名、字段和键类型生成的索引名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'geo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'state'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 移除索引 'geo_state_index'</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="外键约束" tabindex="-1"><a class="header-anchor" href="#外键约束"><span>外键约束</span></a></h3>
<p>Laravel 还支持创建用于在数据库层中的强制引用完整性的外键约束。例如，让我们在 <code v-pre>posts</code> 表上定义一个引用 <code v-pre>users</code> 表的 <code v-pre>id</code> 字段的 <code v-pre>user_id</code> 字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unsigned</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">references</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>还可以为约束的「on delete」和「on update」属性指定所需的操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">references</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">onDelete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cascade'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>dropForeign</code> 方法删除外键。外键约束采用的命名方式与索引相同。即，将数据表名称和约束的字段连接起来，接着加上「_foreign」后缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropForeign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts_user_id_foreign'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，你也可以传递一个字段数组，在删除的时候会按照约定自动转换为对应的外键名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropForeign</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以在迁移文件里使用以下方法来开启或关闭外键约束：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">enableForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">disableForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/hivenkay" target="_blank" rel="noopener noreferrer">@Kevin</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/14940_1491960022.jpeg?imageView2/1/w/100/h/100" alt="100"></td>
<td>翻译</td>
<td>初心易得，始终难守</td>
</tr>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


